package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.tools.downloadtool.DownLoadStatusConfig;
import cn.wit.summit.chwan.utils.APKUtils;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.UtilsMy;
import cn.wit.summit.chwan.widgets.refreshLayout.util.DensityUtil;

public class ItemBestMonthDownButn extends DownloadViewStrokeBig {
    private RoundRelativeLayout rootRoundRl;
    CustomProgressBar progressBar;
    public ItemBestMonthDownButn(Context context) {
        super(context);
    }

    public ItemBestMonthDownButn(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemBestMonthDownButn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.best_month_downbutn_layout, this);
        instalButtomButn = (TextView) view.findViewById(R.id.instalButtomButn);
        rootRoundRl = (RoundRelativeLayout) view.findViewById(R.id.rootRoundRl);
//        rootRoundRl.setRectAdius(DensityUtil.dp2px(20));
        progressBar = view.findViewById(R.id.progressBar);
        setProgressBar(progressBar);
    }

    @Override
    public void updateButn(DownLoadTaskTable downloadTask) {
        int status = 0;
        if (downloadTask != null) {
            boolean isInstall = APKUtils.getInstance(getContext()).checkInstall(getContext(), downloadTask.getPackageName());
            DownLoadTaskTable taskDb= DownloadTaskManager.getInstance().findByGameId(downloadTask.getGameId());
            if (isInstall&&taskDb==null&&downloadTask.getDownStatus()!=DownLoadStatusConfig.UPDATE) downloadTask.setDownStatus(DownLoadStatusConfig.INSTALLED);
            if (downloadTask != null) status = downloadTask.getDownStatus();

        }
        LogUtils.e("statues  2   " + status + "");
        instalButtomButn.setPadding(0,0,0,0);
        progressBar.setVisibility(GONE);
        instalButtomButn.setVisibility(VISIBLE);
        instalButtomButn.setBackgroundResource(R.drawable.installbutn_detial_normal_index_butn);
        instalButtomButn.setTextColor(Color.WHITE);
        if (status == DownLoadStatusConfig.UPDATE) {
        } else if (status == DownLoadStatusConfig.UNZIP) {
            instalButtomButn.setText("解压中");
            instalButtomButn.setTextColor(Color.parseColor("#4BBD6E"));
            instalButtomButn.setBackgroundResource(R.drawable.gamedetial_unziping_rad25_butn);
        } else if (status == DownLoadStatusConfig.UNZIP_ERROR||status==DownLoadStatusConfig.UNZIP_WAIT) {
            instalButtomButn.setText("解压");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_rad25_butn);
        } else if (status == DownLoadStatusConfig.INSTALL_WAIT) {
            instalButtomButn.setText("安装");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_rad25_butn);
        } else if (status == DownLoadStatusConfig.INSTALLING) {
            instalButtomButn.setText("安装中");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
        } else if (status == DownLoadStatusConfig.DOWNLOAD_FINISH) {
        } else if (status == DownLoadStatusConfig.DOWNLOADING || status == DownLoadStatusConfig.START) {
            setProgress(downloadTask);
        } else if (status == DownLoadStatusConfig.DOWNLOAD_STOP || status == DownLoadStatusConfig.DOWNLOAD_ERROR) {
//            instalButtomButn.setText("点击继续");
            setProgress(downloadTask);
        } else if (status == DownLoadStatusConfig.WAIT) {
            instalButtomButn.setText("等待");
        } else if (status == DownLoadStatusConfig.INSTALLED) {
            instalButtomButn.setTextColor(Color.parseColor("#72B0FD"));
            instalButtomButn.setBackgroundResource(R.drawable.gamedetial_open_rad25_butn);
            instalButtomButn.setText("打开");
        } else {
            instalButtomButn.setPadding(0,0,0,0);
            instalButtomButn.setText("秒装");

            if (downloadTask != null) {
                UtilsMy.setIndexButnStatusGamedetial(instalButtomButn, downloadTask.getDownStatus());
            }
        }
    }

    public void setProgress(DownLoadTaskTable downLoadTaskTable) {
        instalButtomButn.setTextColor(Color.parseColor("#999999"));
//        instalButtomButn.setText(progress + "%");
        instalButtomButn.setBackgroundResource(R.drawable.alph);
        instalButtomButn.setVisibility(GONE);
//        instalButtomButn.setAlpha(0);
        progressBar.setVisibility(VISIBLE);
        if (downLoadTaskTable.getDownStatus()==DownLoadStatusConfig.DOWNLOADING
                || downLoadTaskTable.getDownStatus() == DownLoadStatusConfig.START){
        progressBar.setState(102);

        }else {
        progressBar.setState(103);

        }
        progressBar.setProgress((float) downLoadTaskTable.getDownprogress());

    }
}
